package org.linlinjava.litemall.brand.web;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCommodityComment;
import org.linlinjava.litemall.db.domain.LitemallCommodityWithBLOBs;
import org.linlinjava.litemall.db.domain.LitemallReport;
import org.linlinjava.litemall.db.domain.LitemallReportReason;
import org.linlinjava.litemall.db.service.LitemallCommodityCommentService;
import org.linlinjava.litemall.db.service.LitemallCommodityService;
import org.linlinjava.litemall.db.service.LitemallReportReasonService;
import org.linlinjava.litemall.db.service.LitemallReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @description: 商户上传  检测报告
 * @program: mall
 * @author: youvy
 * @create: 2019-12-24 11:04
 **/
@RestController
@RequestMapping("report")
public class ReportController {

	@Autowired
	private LitemallReportService reportService;
	@Autowired
	private LitemallReportReasonService reportReasonService;
	@Autowired
	private LitemallCommodityCommentService commodityCommentService;
	@Autowired
	private LitemallCommodityService commodityService;


	@PostMapping("service/list")
	public Object list(@RequestBody String msg) {
		Integer page = JacksonUtil.parseInteger(msg, "page");
		Integer pageSize = JacksonUtil.parseInteger(msg, "pageSize");
		String type = JacksonUtil.parseString(msg, "type");
		String status = JacksonUtil.parseString(msg, "status");
		if (page == null || pageSize == null) {
			return ResponseUtil.badArgument();
		}
		if (page < 0 || pageSize < 0) {
			return ResponseUtil.badArgumentValue();
		}
		List<LitemallReport> litemallReports = reportService.selectByCondition(page, pageSize, type, status, null,null);
		return ResponseUtil.okPageList(litemallReports,litemallReports);
	}

	@PostMapping("service/brandList")
	public Object brandList(@RequestBody String msg, HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		String belong = user.getPlugininfo().get("belong");

		Integer page = JacksonUtil.parseInteger(msg, "page");
		Integer pageSize = JacksonUtil.parseInteger(msg, "pageSize");
		String type = JacksonUtil.parseString(msg, "type");
		String status = JacksonUtil.parseString(msg, "status");
		if (page == null || pageSize == null) {
			return ResponseUtil.badArgument();
		}
		if (page < 0 || pageSize < 0) {
			return ResponseUtil.badArgumentValue();
		}
		List<LitemallReport> litemallReports = reportService.selectByCondition(page, pageSize, type, status, belong,null);
		return ResponseUtil.okList(litemallReports);
	}

	@PostMapping("service/update")
	public Object update(@RequestBody LitemallReport report) {
		//评论举报通过
		if (report.getStatus() == 102 && report.getType() == 1) {
			LitemallCommodityComment litemallCommodityComment = new LitemallCommodityComment();
			litemallCommodityComment.setId(report.getValueId());
			litemallCommodityComment.setIsDeleted(2);
			commodityCommentService.update(litemallCommodityComment);
		}
		int flag = reportService.update(report);
		if (flag > 0) {
			return ResponseUtil.ok();
		} else {
			return ResponseUtil.fail();
		}
	}

	/**
	 * 新增检测报告
	 * @param report  检测信息:
	 * @param request 携带用户登录信息Authrization
	 * @return
	 */
	@PostMapping("wx/insert")
	public Object insert( @RequestBody LitemallReport report,HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		report.setUserId(Integer.parseInt(user.getUserid()));
		report.setStatus(100);
		if (report.getType() == 1) {
			LitemallCommodityComment litemallCommodityComment = commodityCommentService.selectById(report.getValueId());
			if (litemallCommodityComment.getIsDeleted()!=null) {
				if (litemallCommodityComment.getIsDeleted() == 2) {
					return ResponseUtil.ok();
				}
			}
			litemallCommodityComment.setIsDeleted(1);
			commodityCommentService.update(litemallCommodityComment);
		}else {
			long l = reportService.selectByUIdAndBrandId(Integer.parseInt(user.getUserid()), report.getBrandId());
			if (l>0){
				return ResponseUtil.ok();
			}
		}
		int flag = reportService.insert(report);
		if (flag > 0) {
			return ResponseUtil.ok();
		} else {
			return ResponseUtil.fail();
		}
	}

	/**
	 * 获取  理由表
	 * @return
	 */
	@PostMapping("getReasonList")
	public Object getReasonList() {
		List<LitemallReportReason> list = reportReasonService.list();
		return ResponseUtil.ok(list);
	}

	/**
	 * 详情
	 * @param json
	 * @return
	 */
	@PostMapping("getDetail")
	public Object getDetail(@RequestBody String json){
		Integer type = JacksonUtil.parseInteger(json, "type");
		Integer valueId = JacksonUtil.parseInteger(json, "valueId");
		if (type==null){
			return ResponseUtil.badArgument();
		}
		if (type==1){
		LitemallCommodityComment commodityComment = commodityCommentService.selectById(valueId);
		return ResponseUtil.ok(commodityComment);
		}else {
			LitemallCommodityWithBLOBs commodity= commodityService.selectById(valueId);
			return ResponseUtil.ok(commodity);
		}
	}


}
